<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;
use app\goods\model\ColorGroup as ColorGroupModel;
use app\goods\model\Color as ColorModel;
use app\goods\model\Brand as BrandModel;

/**
 * 色系
 * @package app\admin\controller
 */
class Color extends Admin
{
    /**
    *色系列表
    */
    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $order = $this->getOrder('sort ASC,colorgroupid DESC');
        $data_list = ColorGroupModel::order($order)->select();  

        //新增子类按钮
        
        return ZBuilder::make('table')
        ->addOrder('sort,colorgroupid')
        ->addColumns([ // 批量添加列
            ['sort', '排序'],
            ['groupname', '名称'],
            ['colorgroupvalue1', '色值1'],
            ['colorgroupvalue2', '色值2'],
            ['status', '状态' , 'status'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_colorgroup')
        ->setPrimaryKey('colorgroupid')
        ->addRightButton('custom',['title' => '新增子类', 'class' => 'btn btn-xs btn-default ' ,'icon' => 'list-icon fa fa-plus fa-fw', 'href' => url('addColor', ['id' => '__ID__'])])
        /*->addRightButton('custom',['title' => '查看子类', 'class' => 'btn btn-xs btn-default ' ,'icon' => 'fa fa-fw fa-sitemap', 'href' => url('colorList', ['id' => '__ID__'])])*/
        ->addRightButton('edit')
        ->addTopButton('add')
        ->addRightButton('delete',['href' => url('deletegroup',['ids' => '__ID__'])])
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = ColorGroupModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }


        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'groupname', '名称'],
            ['text', 'sort', '排序'],
            ['text', 'colorgroupvalue1', '色值1'],
            ['text', 'colorgroupvalue2', '色值2'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->layout(['groupname' => 6, 'sort' => 6, 'colorgroupvalue1' => 6, 'colorgroupvalue2' => 6])
        ->fetch();
    }

    /**
    *修改
    */
    public function edit($id = 0){
        if ($id === 0) return $this->error('缺少参数');

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = ColorGroupModel::update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('修改失败');
            }
        }

        //详情
        $info      = ColorGroupModel::where('colorgroupid',$id)->find();

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列 
            ['hidden', 'colorgroupid'],           
            ['text', 'groupname', '名称'],
            ['text', 'colorgroupvalue1', '色值1'],
            ['text', 'colorgroupvalue2', '色值2'],
            ['text', 'sort', '排序'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->setFormData($info)
        ->layout(['groupname' => 6, 'sort' => 6, 'colorgroupvalue1' => 6, 'colorgroupvalue2' => 6])
        ->fetch();
    }


    /**
    *新增颜色
    */
    public function addColor(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = ColorModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
        $id = input('id');

        //上级列表
        $groupList = ColorGroupModel::order(array('sort' => 'ASC'))->column('colorgroupid,groupname');

        //品牌列表
        $brandList = BrandModel::where('status', '1')->order(array('sort' => 'ASC'))->column('brandid,brandname');

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['select', 'colorgroupid', '所属色系', '<span class="text-danger">必选</span>', $groupList,$id],
            ['select', 'brandid', '品牌', '<span class="text-danger">必选</span>', $brandList],             
            ['text', 'name', '名称'],
            ['text', 'customcode', '自编码'],
            ['text', 'colorvalue1', '色值1'],
            ['text', 'colorvalue2', '色值2'],
            ['text', 'sort', '排序'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->layout(['colorgroupid' => '6', 'brandid' => '6', 'name' => '6', 'customcode' => '6', 'colorvalue1' => '6', 'colorvalue2' => '6', 'sort' => '6', 'status' => '6', ])
        ->fetch();
    }


    /**
    *颜色列表
    */
    public function colorList(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $map = $this->getMap();
        $data_list = ColorModel::getAll($map, 'sort ASC,colorgroupid,brandid,colorid DESC'); 
        
        return ZBuilder::make('table')
        ->setSearch(['goods_color.colorid' => 'ID', 'goods_colorgroup.groupname' => '色系', 'goods_brand.brandname' =>'品牌','goods_color.Name' => '名称', 'goods_color.customcode' => '自编码'])
        ->addColumns([ // 批量添加列
            ['sort', '排序'],
            ['groupname', '色系'],
            ['brandname', '品牌'],
            ['name', '名称'],
            ['customcode', '自编码'],
            ['colorvalue1', '色值1'],
            ['colorvalue2', '色值2'],
            ['status', '状态' , 'status'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_color')
        ->setPrimaryKey('colorid')
        ->addRightButton('edit', ['href' => url('editColor', ['id' => '__id__'])])
        ->addTopButton('add', ['href' => url('addColor')])
        ->addRightButton('delete')
        ->addTopButton('custom', ['title' => '修改所属色系', 'icon' => 'fa fa-fw fa-pencil-square-o', 'href'=> url('editgroup'), 'class' => 'btn btn-danger js-get'])
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *修改颜色
    */
    public function editColor($id = 0){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = ColorModel::update($data);
            if($result) {
                $res = db('goods_list')->where('colorid', $data['colorid'])->update(['colorgroupid' => $data['colorgroupid']]);
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('无修改');
            }
        }

        if($id == 0){
            $this->error = '缺少参数';
        }
        //上级列表
        $groupList = ColorGroupModel::order(array('sort' => 'ASC'))->column('colorgroupid,groupname');

        //品牌列表
        $brandList = BrandModel::where('status', '1')->order(array('sort' => 'ASC'))->column('brandid,brandname');

        //详情
        $info      = ColorModel::where('colorid', $id)->find();

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['hidden', 'colorid'],
            ['select', 'colorgroupid', '所属色系', '<span class="text-danger">必选</span>', $groupList, $info['colorgroupid']],
            ['select', 'brandid', '品牌', '<span class="text-danger">必选</span>', $brandList, $info['brandid']],             
            ['text', 'name', '名称'],
            ['text', 'customcode', '自编码'],
            ['text', 'colorvalue1', '色值1'],
            ['text', 'colorvalue2', '色值2'],
            ['text', 'sort', '排序'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->setFormData($info)
        ->layout(['colorgroupid' => '6', 'brandid' => '6', 'name' => '6', 'customcode' => '6', 'colorvalue1' => '6', 'colorvalue2' => '6', 'sort' => '6', 'status' => '6', ])
        ->fetch();
    }

    /**
    *删除色系
    */
    public function deletegroup($ids = 0){
        $id     = input('ids');
        $result = ColorGroupModel::where(array('colorgroupid' => $id))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
    *删除颜色
    */
    public function delete($ids = 0){
        $id     = input('ids');
        $result = ColorModel::where(array('colorid' => $id))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
    *修改所属色系
    */
    public function editgroup(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['colorgroupid'])){
                $this->error('请选择色系');
            }
            $colorid = explode(',', trim($data['ids'],','));
            foreach ($colorid as $key => $value) {
                $result = ColorModel::where('colorid',$value)->update(array('colorgroupid'=>$data['colorgroupid']));
                $res = db('goods_list')->where('colorid', $value)->update(['colorgroupid' => $data['colorgroupid']]);
            }
            if($result){
                $this->success('修改成功',cookie('__forward__'));
            }
        }

        //获取修改数据
        $ids = input('ids');

        //色系列表
        $group_list = ColorGroupModel::where('status','1')->column('colorgroupid,groupname');

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列
            ['hidden', 'ids',$ids],
            ['select', 'colorgroupid', '色系', '<span class="text-danger">必选</span>', $group_list],                  
            
        ])
        ->layout(['colorgroupid' => '6' ])
        ->fetch();

    }


   

    
   
}
